﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UseCase.Validation.cs" company="">
//   
// </copyright>
// <summary>
//   The use case.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.UseCases
{
    using System;
    using System.Collections.Generic;

    using Libium.Validation;

    /// <summary>
    ///   The use case.
    /// </summary>
    public abstract partial class UseCase
    {
        #region Public Events

        /// <summary>
        ///   The error added.
        /// </summary>
        public event Action<IUseCase, IError> ErrorAdded;

        /// <summary>
        ///   The error deleted.
        /// </summary>
        public event Action<IUseCase, IError> ErrorDeleted;

        /// <summary>
        ///   The hint added.
        /// </summary>
        public event Action<IUseCase, IHint> HintAdded;

        /// <summary>
        ///   The hint deleted.
        /// </summary>
        public event Action<IUseCase, IHint> HintDeleted;

        /// <summary>
        ///   The warning added.
        /// </summary>
        public event Action<IUseCase, IWarning> WarningAdded;

        /// <summary>
        ///   The warning deleted.
        /// </summary>
        public event Action<IUseCase, IWarning> WarningDeleted;

        #endregion

        #region Public Properties

        /// <summary>
        ///   Gets the errors.
        /// </summary>
        public IEnumerable<IError> Errors
        {
            get
            {
                return this._errors;
            }
        }

        /// <summary>
        ///   Gets the hints.
        /// </summary>
        public IEnumerable<IHint> Hints
        {
            get
            {
                return this._hints;
            }
        }

        /// <summary>
        ///   Gets a value indicating whether valid.
        /// </summary>
        public bool Valid
        {
            get
            {
                return this._validationRecords.Count == 0;
            }
        }

        /// <summary>
        ///   Gets the validation records.
        /// </summary>
        public IEnumerable<ValidationRecord> ValidationRecords
        {
            get
            {
                return this._validationRecords;
            }
        }

        /// <summary>
        ///   Gets the warnings.
        /// </summary>
        public IEnumerable<IWarning> Warnings
        {
            get
            {
                return this._warnings;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The add error.
        /// </summary>
        /// <param name="text">
        /// The text. 
        /// </param>
        /// <param name="helpUri">
        /// The help uri. 
        /// </param>
        public void AddError(string text, string helpUri = null)
        {
            var error = new Error { Text = text, HelpUri = helpUri };

            this._errors.Add(error);
            this.OnErrorAdded(error);
        }

        /// <summary>
        /// The add hint.
        /// </summary>
        /// <param name="text">
        /// The text. 
        /// </param>
        /// <param name="helpUri">
        /// The help uri. 
        /// </param>
        public void AddHint(string text, string helpUri = null)
        {
            var hint = new Hint { Text = text, HelpUri = helpUri };

            this._hints.Add(hint);
            this.OnHintAdded(hint);
        }

        /// <summary>
        /// The add warning.
        /// </summary>
        /// <param name="text">
        /// The text. 
        /// </param>
        /// <param name="helpUri">
        /// The help uri. 
        /// </param>
        public void AddWarning(string text, string helpUri = null)
        {
            var warning = new Warning { Text = text, HelpUri = helpUri };

            this._warnings.Add(warning);
            this.OnWarningAdded(warning);
        }

        /// <summary>
        /// The delete error.
        /// </summary>
        /// <param name="error">
        /// The error. 
        /// </param>
        public void DeleteError(IError error)
        {
            this._errors.Remove(error);
            this.OnErrorDeleted(error);
        }

        /// <summary>
        /// The delete hint.
        /// </summary>
        /// <param name="hint">
        /// The hint. 
        /// </param>
        public void DeleteHint(IHint hint)
        {
            this._hints.Remove(hint);
            this.OnHintDeleted(hint);
        }

        /// <summary>
        /// The delete warning.
        /// </summary>
        /// <param name="warning">
        /// The warning. 
        /// </param>
        public void DeleteWarning(IWarning warning)
        {
            this._warnings.Remove(warning);
            this.OnWarningDeleted(warning);
        }

        /// <summary>
        ///   The validate.
        /// </summary>
        public virtual void Validate()
        {
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on error added.
        /// </summary>
        /// <param name="error">
        /// The error. 
        /// </param>
        protected virtual void OnErrorAdded(IError error)
        {
            if (this.ErrorAdded != null)
            {
                this.ErrorAdded(this, error);
            }
        }

        /// <summary>
        /// The on error deleted.
        /// </summary>
        /// <param name="error">
        /// The error. 
        /// </param>
        protected virtual void OnErrorDeleted(IError error)
        {
            if (this.ErrorDeleted != null)
            {
                this.ErrorDeleted(this, error);
            }
        }

        /// <summary>
        /// The on hint added.
        /// </summary>
        /// <param name="hint">
        /// The hint. 
        /// </param>
        protected virtual void OnHintAdded(IHint hint)
        {
            if (this.HintAdded != null)
            {
                this.HintAdded(this, hint);
            }
        }

        /// <summary>
        /// The on hint deleted.
        /// </summary>
        /// <param name="hint">
        /// The hint. 
        /// </param>
        protected virtual void OnHintDeleted(IHint hint)
        {
            if (this.HintDeleted != null)
            {
                this.HintDeleted(this, hint);
            }
        }

        /// <summary>
        /// The on warning added.
        /// </summary>
        /// <param name="warning">
        /// The warning. 
        /// </param>
        protected virtual void OnWarningAdded(IWarning warning)
        {
            if (this.WarningAdded != null)
            {
                this.WarningAdded(this, warning);
            }
        }

        /// <summary>
        /// The on warning deleted.
        /// </summary>
        /// <param name="warning">
        /// The warning. 
        /// </param>
        protected virtual void OnWarningDeleted(IWarning warning)
        {
            if (this.WarningDeleted != null)
            {
                this.WarningDeleted(this, warning);
            }
        }

        #endregion
    }
}